﻿
using ServerBase.Config;
using SvGame.Logic;
using SvGame.Objects;

namespace SvGame.Controller;

public partial class GameHandler
{
    /// <summary>
    /// 释放perform
    /// </summary>
    /// <param name="pack"></param>
    public int C2GPerformAct(MyPackage pack)
    {
        pack.GetMsg(out C2GPerformAct req);
        var rsp = new G2CPerformAct();

        rsp.Perform = req.Perform;
        if (!pack.PackGetPlayer(out var player) || player is null)
        {
            return 0;
        }

        if (!ConfPerform.TryGetValue(req.Perform, out var conf))
        {
            return player.MessageError("你要施放什么技法？");
        }
        if (!player.Room.CanFight())
        {
            return player.MessageError("这里禁止战斗。");
        }
        if (player.IsBusy)
        {
            return player.MessageError(BusyMessage);
        }

        if ((EPerformMoment)conf.PerformMoment == EPerformMoment.非战斗中)
        {
            if (player.IsFighting())
            {
                return player.MessageError($"你还在战斗中，不能施放『{conf.Name}』！");
            }
        }
        // 判断技能需求
        foreach (var kvp in conf.SkillCondition)
        {
            if (player.SkillGetLevel(kvp.Key) < kvp.Value)
            {
                return player.MessageError($"你的『{kvp.Key}』等级不足，不能施放『{conf.Name}』！");
            }
        }
        // 属性判断需求
        foreach (var kvp in conf.PropCondition)
        {
            if (player.GetProp((EPropType)kvp.Key) < kvp.Value)
            {
                return player.MessageError($"你的『{((EPropType)kvp.Key).GetPropName()}』不足，不能施放『{conf.Name}』！");
            }
        }


        pack.Send(rsp);

        var result = player.PerformAct(req.Perform);
        if (result == 0)
        {
            // 属性消耗
            foreach (var kvp in conf.PropCondition)
            {
                player.UseProp((EPropType)kvp.Key, kvp.Value);
            }
            // 自busy
            player.BusyStart(conf.Busy.RandomRange() * 1000);
            // CD时间
            player.CdStart(conf.Id, conf.CoolDown);
        }

        return result;
    }
}
